package com.zt.rongda.stock.service;

import com.zt.rongda.stock.inter.StockTransferOutMapper;
import com.zt.rongda.stock.model.Stock;
import com.zt.rongda.stock.model.StockTransferIn;
import com.zt.rongda.stock.model.StockTransferOut;
import com.zt.servlet.ReqBean;
import com.zt.system.model.BusinessException;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.DateUtil;
import com.zt.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class StockTransferOutService extends BaseService {

    @Autowired
    private StockTransferOutMapper stockTransferOutMapper;
    @Autowired
    private StockService stockService;

    /**
    * 查询
    *
    * @param reqBean
    * @return
    */
    public JSONObject getTable(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");
        sqlBuilder.appendSql("select a.LIST_ID, a.STOCK_ID, a.STOCK_NAME, a.TYPE_ID, a.TYPE_NAME, a.SPEC, a.BRAND, a.UNIT, a.NUM, a.PRICE, a.TOTAL_PRICE, a.LOCATION, a.PURPOSE, a.CONTENT, a.USER_ID, b.EMP_NAME USER_NAME, a.MAKE_TIME, a.REMARK from STOCK_TRANSFER_OUT a LEFT JOIN EMPLOYEE b ON a.USER_ID = b.EMP_ID  ");

        sqlBuilder.addWhereLike2("STOCK_NAME", reqBean.getInfo("stockName"));
        sqlBuilder.addWhereLike2("TYPE_ID", reqBean.getInfo("typeId"));

        // 时间间隔
        String endDate = reqBean.getInfo("makeTimeEnd");
        if(endDate != null && endDate.length()>=10)
        {
            Date tmp_date =  DateUtil.parse(endDate);

            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            endDate = df.format(new Date(tmp_date.getTime() + 1 * 24 * 60 * 60 * 1000)); // 结束日期加一天
        }
        sqlBuilder.addTimeWhere("MAKE_TIME", reqBean.getInfo("makeTimeStart"), ">=");
        sqlBuilder.addTimeWhere("MAKE_TIME", endDate, "<");
        //

        return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
    * 通过id查询
    *
    * @param listId
    * @return
    */
    public StockTransferOut getById(String listId) {
        return stockTransferOutMapper.selectByPrimaryKey(listId);
    }

    /**
    * 新增
    *
    * @param stockTransferOut
    */
    public void add(StockTransferOut stockTransferOut) {
        stockTransferOut.setListId(stringId());// 手动设置Id

        stockTransferOut.setMakeTime(new Date());
        stockTransferOut.setModifyTime(stockTransferOut.getMakeTime());
        stockTransferOutMapper.insert(stockTransferOut);

        //库存减少
        Stock stock = getStock(stockTransferOut);
        if(stockService.isExisted(stock) && stockService.isEnough(stock)){
            stockService.decrease(stock);
        }
    }

    /**
    * 修改
    *
    * @param stockTransferOut
    */
    public void update(StockTransferOut stockTransferOut) {

        //库存更新
        StockTransferOut oldStockTransferOut = stockTransferOutMapper.selectByPrimaryKey(stockTransferOut.getListId());
        Stock oldStock = getStock(oldStockTransferOut);
        if(stockService.isExisted(oldStock) ){
            stockService.increase(oldStock);
        }

        Stock stock = getStock(stockTransferOut);
        if(stockService.isExisted(stock) && stockService.isEnough(stock)){
            stockService.decrease(stock);
        }

        //更新入库记录
        String listId = stockTransferOut.getListId();
        if (StringUtil.isEmpty(listId)) {
            throw new BusinessException("获取主键失败");
        }

        stockTransferOut.setModifyTime(new Date());
        stockTransferOutMapper.updateByPrimaryKeySelective(stockTransferOut);
    }

    /**
    * 删除
    *
    * @param stockTransferOut
    */
    public void delete(StockTransferOut stockTransferOut) {

        String listId = stockTransferOut.getListId();
        if (StringUtil.isEmpty(listId)) {
            throw new BusinessException("获取主键失败");
        }
        stockTransferOutMapper.deleteByPrimaryKey(listId);

        //库存增加
        Stock stock = getStock(stockTransferOut);

        if(stockService.isExisted(stock)){
            stockService.increase(stock);
        }
    }

    /**
     * 获取StockTransferOut对象
     *
     * @param stockTransferOut
     */
    private Stock getStock(StockTransferOut stockTransferOut){
        Stock stock = new Stock();
        stock.setListId(stockTransferOut.getListId());
        stock.setStockId(stockTransferOut.getStockId());
        stock.setStockName(stockTransferOut.getStockName());
        stock.setTypeId(stockTransferOut.getTypeId());
        stock.setTypeName(stockTransferOut.getTypeName());
        stock.setUnit(stockTransferOut.getUnit());
        stock.setBrand(stockTransferOut.getBrand());
        stock.setSpec(stockTransferOut.getSpec());
        stock.setAmount(stockTransferOut.getNum());

        return stock;
    }

}